package com.sdhs.system.syncdata.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.filter.ValueFilter;
import com.banboocloud.Codec.BamboocloudFacade;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmy
 * @date 2022/9/2311:23
 * @Description: 转换返回值
 * flag 是否需要替换【】 1需要 2不需要
 */
public class TransforUtil {
    private static ValueFilter valueFilter = (o, s, o1) -> o1 == null ? "" : o1;
    public   static Object getJsonObject(ResultUser result, String newName,int flag) {
        List list= (List) result.getUserIdList();

        String s = JSON.toJSONString(result, valueFilter);
        String replace="";
        if(list!=null&&list.size()!=0){
            replace = getString(newName, flag, s);

        }else{
            replace=getString(newName,flag,s);

        }
        System.out.println("replace:"+replace);
//        return replace;
        return JSON.parseObject(replace);
    }

    private static String getString(String newName, int flag, String s) {
        String replace="";
        switch (flag){
            case 1:
                replace = s.replace("userIdList", newName).replace("[","").replace("]","");
                break;
            case 2:
                replace = s.replace("userIdList", newName);
                break;
            case 3:
                replace = s.replace("userIdList", newName).replace("[","{").replace("]","}");
                break;
            case 4:
                replace = s.replace("userIdList", newName).replace("[{","{").replace("}]","}");
                break;
        }
        return replace;
    }

    /**
     *
     * @param random  新生成的用户或机构id
     * @param id  新生成的用户或机构id
     * @param insert 添加成功的条数
     * @param replaceStr 替换成
     * @param replaceFlag 字符串替换方法   为8时，默认是username唯一索引冲突
     * @return
     */
    public static Object getResult(String random, String id, int insert, String replaceStr,int replaceFlag) {
        ResultUser resultUser=new ResultUser();
        if(replaceFlag==8&&insert==0){
            Result result=new Result();
            result.setBimRequestId(random);
            result.error("账号已存在，原手机号为"+replaceStr,1);
            return BamboocloudFacade.encrypt(JSONUtil.toJsonStr(result), "123456", "AES");
//            return result;
        }
        if(insert==1){
            List<String> list=new ArrayList<>();
            list.add(id);
            resultUser.setBimRequestId(random);
            resultUser.setUserIdList(list);
            Object jsonObject= TransforUtil.getJsonObject(resultUser,replaceStr,replaceFlag);
            return BamboocloudFacade.encrypt(JSONUtil.toJsonStr(jsonObject), "123456", "AES");
//            return jsonObject;
        }else{
            Result result=new Result();
            result.setBimRequestId(random);
            result.error("账号创建失败");
            return BamboocloudFacade.encrypt(JSONUtil.toJsonStr(result), "123456", "AES");
//            return result;
        }
    }
}
